#!/bin/csh -f
#
#         C-shell script : RNX2CRZ
#             (frontend of RNX2CRX)
#                 1996.12.19  created by Y. Hatanaka.
#                 2007.06.10  updated by Y. Hatanaka.
#                      - file name extension may be lower and upper case
#                 2009.07.07  modified extensively by Y. Hatanaka.
#                      - Ambiguities/bugs in following items are resolved:
#                         * setting of output directory
#                         * case of deletion of input files
#                         * case of overwriting an output file
#                        Options to control them are added.
#                      - Handling of more RINEX file types are added.
#                      - 'gzipped' OBS files can be processed.
#                      - Optionnaly, gzip is applicable instead of
#                        UNIX compress.
#                 2014.03.24  modified by Y. Hatanaka.
#                      - Manipulation of file names in the new file naming
#                        convention (*.rnx/crx) is added.
#
#--------------------------------------------------------------------
set help = 0
foreach var ($argv[*])
   if ( "$var" == '-h' ) set help = 1
end

if($#argv < 1 || $help) then
more << EOF

RNX2CRZ: C-shell script to compress multiple RINEX files.

Usage : RNX2CRZ [-c] [-d] [-g] [-f] [-v] [-h] file ...

       -c : output to the current directory
       -d : delete the input file if the compressed successfully
       -g : apply "gzip" (default: UNIX compress)
       -f : force overwriting output files without inquiring
       -v : verbose mode
       -h : show this message and stop
       file ... : input RINEX (or uncompressed CRINEX) files.
                  Wildcards can be used.

          RINEX           -->    CRINEX      --> compressed RINEX/CRINEX
                               ????????.??d  -->    ????????.??d.Z
       ????????.??o       --> (????????.??d) -->    ????????.??d.Z
       ????????.??o.Z(gz) --> (????????.??d) -->    ????????.??d.Z
       ????????.??n                          -->    ????????.??n.Z
       ????????.??g                          -->    ????????.??g.Z
       ????????.??l                          -->    ????????.??l.Z
       ????????.??p                          -->    ????????.??p.Z
       ????????.??h                          -->    ????????.??h.Z
       ????????.??b                          -->    ????????.??b.Z
       ????????.??m                          -->    ????????.??m.Z
       ????????.??c                          -->    ????????.??C.Z
       *.?O.rnx                              -->    *.?O.crx.Z
       *.?[NM].rnx                           -->    *.[NM]?.rnx.Z

Remarks:
  - Installation of RNX2CRX is necessary to use this tool.
  - The extensions of the input files must conform to the RINEX convention.
  - A compressed file is saved in the same directory as the input file
    unless the option '-c' is specified.
  - An input file is deleted only when the option "-d" is specified and 
    the compression is successful.

   [20090707]

EOF

exit
endif
#--------------------------------------------------------------------

# set default mode
set out_to_current_dir = 0
set delete_input_files = 0
set force_overwrite = 0
unset verbose
set COMPRESS = compress
set EXT = 'Z'

unset noclobber

# check options
foreach var ($argv[*])
   switch ($var)
     case '-c':
       set out_to_current_dir = 1
       shift; breaksw
     case '-d':
       set delete_input_files = 1
       shift; breaksw
     case '-g':
       set COMPRESS = gzip
       set EXT = 'gz'
       shift; breaksw
     case '-f':
       set force_overwrite = 1
       shift; breaksw
     case '-v':
       set verbose = 1
       shift; breaksw
     default:
       break
   endsw
end 

# process files
foreach file_in ($argv[*])

    # compose name of output file (excluding ".Z" or ".gz")
    if ( $file_in =~ *.Z || $file_in =~ *.gz ) then
        set file = $file_in:r  # remove ".Z" or ".gz"
        set CAT = zcat
    else
        set file = $file_in
        set CAT = cat;
    endif
    if ( $out_to_current_dir ) set file = $file:t  # remove path
    if      ( $file =~ *.??[oO] ) then
        set file_out = `echo $file | sed -e 's/o$/d/' -e 's/O$/D/' `
    else if ( $file =~ *[oO].rnx || $file =~ *[oO].RNX ) then
        set file_out = `echo $file | sed -e 's/rnx$/crx/' -e 's/RNX$/CRX/' `
    else if ( $file_in =~ *.??[dDnNgGlLpPhHbBmMcC]   ||  \
              $file_in =~ *.rnx || $file_in =~ *.RNX ||  \
              $file_in =~ *.crx || $file_in =~ *.CRX ) then
        set file_out = $file
    else
        echo "Skip $file_in. It's already compressed or the file name doesn't fit to the naming convention."
        continue
    endif

    # check if the output file is preexisting
    if ( -e "$file_out.$EXT" && ! $force_overwrite ) then
        echo "The file $file_out already exists. Overwrite?(y/n,default:n)"
        if ( $< !~ [yY] ) continue
    endif

    # issue the command
    if      ( $file =~ *.??[oO] || $file =~ *[oO].rnx || $file =~ *[oO].RNX ) then
        $CAT $file_in | RNX2CRX - | $COMPRESS -c > $file_out.$EXT
    else if ( $file_in =~ *.??[dDnNgGlLpPhHbBmMcC]   || \
              $file_in =~ *.rnx || $file_in =~ *.RNX || \
              $file_in =~ *.crx || $file_in =~ *.CRX) then
        $COMPRESS -c $file_in > $file_out.$EXT
    else
        continue
    endif

    # remove the input file
    if ( $status == 0 && $delete_input_files ) rm $file_in

end
